// index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currProduct: [],
    selList: "",
    pmId: "",
    storeid: app.globalData.storeId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    var that = this;
    var selList = e.selList;
    that.setData({
      selList: selList,
      pmId: e.pmid
    })
    that.getGift(e.pmid);
  },

  // 获得满赠赠品http://weapp.711688.net.cn/api/Cart/GetFullSend?pmId=11
  getGift: function(pmid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Cart/GetFullSend?pmId=' + pmid,
      success: function(res) {
        if (res.data.Code == 0) {
          that.setData({
            currProduct: res.data.Data
          })
        }
      }
    })
  },

  //赠品加入购物车
  addcart: function(e) {
    this.delectGift();
    var pid = e.currentTarget.dataset.pid;
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Cart/AddFullSend',
      data: {
        pmId: that.data.pmId,
        pid: pid,
        uid: app.globalData.userInfo.Uid,
        selList: that.data.selList
      },
      success: function(res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '选择赠品成功',
            icon: 'success',
            duration: 2000,
            success: function() {
              wx.navigateBack({
                delta: 1,
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          });
          return;
        }
      }
    })
  },

  // 删除购物车中满赠商品POST /api/Cart/DelFullSend
  delectGift: function() {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/Cart/DelFullSend',
      data: {
        pmId: that.data.pmId,
        uid: app.globalData.userInfo.Uid,
        selList: that.data.selList
      },
      success: function(res) {
        if (res.data.Code == 0) {}
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  }
})